package com.qingjiang.projectschedule.utils;

public class Result<T> {
    private Integer code;
    private String message;

    private Boolean success;
    private T data;

    public Result() {}

    protected static <T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if(data!=null) result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body,Integer code,String message,Boolean success){
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(success);
        return result;
    }

    public static <T> Result<T> build(T body,ResultCodeEnum resultCodeEnum){
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setSuccess(resultCodeEnum.getSuccess());
        return result;
    }

    public static <T> Result<T> ok(T body){
        return build(body,ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> error(T body) { return build(body,404,"error",false); }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setSuccess(Boolean success){ this.success = success; }

    public Boolean getSuccess() {return success; }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
